package com.cheer.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.dto.MallComplaintQueryDTO;
import com.cheer.entity.MallComplaintEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 投诉表
 *
 * @author cheer
 */
@Mapper
@Repository
public interface MallComplaintDao extends BaseMapper<MallComplaintEntity> {

    default IPage<MallComplaintEntity> selectListByQueryCondition(MallComplaintQueryDTO complaintQueryDTO) {
        QueryWrapper<MallComplaintEntity> queryWrapper = new QueryWrapper<>();

        String memberInfo = StringUtils.trimToNull(complaintQueryDTO.getMemberInfo());
        if (memberInfo != null) {
            queryWrapper.like("real_name", memberInfo);
        }

        Date fromDate = complaintQueryDTO.getFromDate();
        if (fromDate != null) {
            queryWrapper.ge("create_time", fromDate);
        }

        Date toDate = complaintQueryDTO.getToDate();
        if (toDate != null) {
            queryWrapper.lt("create_time", DateUtils.addDays(toDate, 1));
        }

        if (complaintQueryDTO.getStatus() != null) {
            queryWrapper.eq("status", complaintQueryDTO.getStatus());
        }

        return selectPage(new Page<>(complaintQueryDTO.getPageNum(), complaintQueryDTO.getPageSize()), queryWrapper);
    }

    default List<MallComplaintEntity> selectListByUserId(Integer userId) {
        return selectList(new QueryWrapper<MallComplaintEntity>()
                .eq("user_id", userId)
                .orderByDesc("create_time"));
    }
}
